<?php
class App_Source_Image_Mapper extends Mozg_Mapper_Data
{
	private $_uploadError;

    public function GetImageThumbFolder($thumbFolder, $realFolder = false)
    {
        if (!$this->IsThumbFolder($thumbFolder)) throw new Mozg_Exception_System(Mozg_Lang::_('DIRECTORY_%_NOT_FOUND', $thumbFolder));
        
        $realFolder = trim($realFolder, '/');
        if (strlen($realFolder)) $realFolder .= '/';
        return _CACHE_FOLDER_THUMB.$thumbFolder. '/' .$realFolder;
    }
    
    public function IsThumbFolder($thumbFolder)
    {
    	$thumbSizes = Mozg_Cfg::Get('global/image')->GetThumbSizes();
        return isset($thumbSizes[$thumbFolder]);
    }
    
    public function GetThumbFolderSizes($thumbFolder)
    {
        if (!$this->IsThumbFolder($thumbFolder)) throw new Mozg_Exception_System(Mozg_Lang::_('DIRECTORY_%_NOT_FOUND', $thumbFolder));
        
        $thumbSizes = Mozg_Cfg::Get('global/image')->GetThumbSizes();
        return $thumbSizes[$thumbFolder];
    }
    
    public function UploadResize($dstPath, $newFileName, $uploadedFilePath, $maxSize)
    {
		$imageFile = Mozg::Lib('image')->GetImage($uploadedFilePath);
		
		$this->SetUploadError();
		
		if (!$imageFile->IsImage())
		{
		    $this->SetUploadError($imageFile->GetError());
		    return false;
		}

		$newFullFileName = $imageFile->Resize($dstPath, $newFileName, $maxSize);
		
		if ($imageFile->IsError())
		{
		    $this->SetUploadError($imageFile->GetError());
		    return false;
		}

		return $newFullFileName;
    }
    
    private function SetUploadError($message = '')
    {
        $this->_uploadError = $message;
    }

    public function GetUploadError()
    {
        return $this->_uploadError;
    }

    private function GetUploadErrorMessage($errorCode)
    {
        switch ($errorCode)
        {
            case UPLOAD_ERR_FORM_SIZE:
            case UPLOAD_ERR_INI_SIZE:
	            $m = Mozg_Lang::_('UPLOAD_FILE_SIZE_EXCEEDED');
	            break;
            
            case UPLOAD_ERR_PARTIAL:
                $m = Mozg_Lang::_('FILE_WAS_UPLOADED_PARTIALLY');
                break;
            
            case UPLOAD_ERR_NO_FILE:
                $m = Mozg_Lang::_('FILE_WAS_NOT_UPLOADED');
                break;
            
            default: $m = Mozg_Lang::_('UNKNOWN_ERROR');
                                    
        }
        
        return $m;
    }
    
}